// pages/myShare/detail/detail.js
import * as API from '../../../api/myShareApi';
import * as util from '../../../utils/util';

Page ({
  /**
   * 页面的初始数据
   */
  data: {
    // num: 68,
    shareModual: false,
    editState: false,
    customCode: '',
    customName: '',
    orderList: [], //列表
    editParams: [], //编辑
    postParams: [], //微信转发
    // shortLinkParams: [],
    shareId: '',
    promised: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},
  getChooseOrderListById: function () {
    let customId = wx.getStorageSync ('customId');
    let orderIdList = wx.getStorageSync ('selectOrder');
    let postBody = {
      customId: customId,
      orderIdList: orderIdList,
    };
    API.chooseOrderListById (postBody).then (
      res => {
        if (res && res.orderList) {
          if (res.orderList.length > 0) {
            let data = {
              customCode: res.orderList[0].customCode,
              customName: res.orderList[0].customName,
              orderList: res.orderList,
              postParams: [],
            };
            data.orderList.map (item => {
              let obj = {
                orderId: item.id,
                customId: item.customId,
                isHiddenPrice: item.hiddenPrice,
                platform: '1',
              };
              data.postParams.push (obj);
              item.showBtn = false;
            });
            this.setData (data);
          }
        }
      },
      err => {
        setTimeout (() => {
          wx.navigateBack ({
            delta: 1,
          });
        }, 1500);
      }
    );
  },
  shareList: function () {
    this.setData ({
      shareModual: true,
    });
    API.preShare (this.data.postParams).then (
      res => {
        this.setData ({
          shareId: res.shareId,
        });
      },
      err => {
        util.toast ('获取分享id失败');
      }
    );
  },
  hideShareModal: function () {
    this.setData ({
      shareModual: false,
    });
  },
  //
  toOrderDetail: function (e) {
    let id = e.currentTarget.dataset.id;
    let hiddenPrice = e.currentTarget.dataset.hiddenprice;
    if (!this.data.editState) {
      util.preventMoreClick (this, () => {
        wx.navigateTo ({
          url: '/pages/order/orderDetail/orderDetail?id=' + id,
        });
      });
    } else {
      this.showTap (id, hiddenPrice);
    }
  },
  toCustomer: function () {
    util.preventMoreClick (this, () => {
      wx.navigateTo ({
        url: '/pages/myShare/myShare?fromDetail=1',
      });
    });
  },
  toOrder: function () {
    let customId = wx.getStorageSync ('customId');
    util.preventMoreClick (this, () => {
      wx.redirectTo ({
        url: '/pages/myShare/order/order?customId=' + customId,
      });
    });
  },
  // 操作点击样式
  showTap: function (id, hiddenPrice) {
    let customId = wx.getStorageSync ('customId');
    let data = {
      orderList: util.deepClone (this.data.orderList),
      editParams: util.deepClone (this.data.editParams),
    };
    data.orderList.forEach ((item, index) => {
      if (item.id == id) {
        console.log (index);
        if (item.showBtn) {
          data.editParams.forEach ((elem, num) => {
            if (elem.orderId == item.id) {
              data.editParams.splice (num, 1);
            }
          });
          item.showBtn = false;
        } else {
          let obj = {
            orderId: id,
            customId: customId,
            isHiddenPrice: hiddenPrice,
            platform: '1',
          };
          data.editParams.push (obj);
          item.showBtn = true;
        }
      }
    });
    console.log (data.editParams);
    this.setData (data);
  },
  edit: function () {
    this.setData ({
      editState: true,
    });
  },
  // 隐藏价格
  hiddenCost: function () {
    let len = this.data.editParams;

    let data = {
      orderList: this.data.orderList,
      editParams: this.data.editParams,
      postParams: this.data.postParams,
    };
    if (len < 1) {
      util.toast ('请至少选择一条');
    } else {
      data.editParams.forEach ((element, index) => {
        data.orderList.map ((item, num) => {
          if (element.orderId == item.id) {
            element.isHiddenPrice = true;
            item.hiddenPrice = true;
          }
        });
      });
      data.editParams.forEach ((element, index) => {
        data.postParams.map ((item, num) => {
          if (element.orderId == item.orderId) {
            item.isHiddenPrice = true;
          }
        });
      });
      this.setData (data);
      util.toast ('价格已隐藏');
    }
  },
  // 删除
  delectOrder: function () {
    // let edit = util.deepClone(this.data.editParams);
    // let post = util.deepClone(this.data.postParams);
    let len = this.data.editParams.length;
    let postLen = this.data.postParams.length;

    if (len < 1) {
      util.toast ('请至少选择一条');
    } else {
      if (postLen == len) {
        util.toast ('不能全部删除,至少含有一条分享订单');
      } else {
        for (let i = 0; i < len; i++) {
          this.changeOrder ();
        }
      }
    }
  },
  changeOrder: function () {
    let editArray = util.deepClone (this.data.editParams);
    let orderArray = util.deepClone (this.data.orderList);
    let postArray = util.deepClone (this.data.postParams);
    var data = {
      orderList: orderArray,
      editParams: editArray,
      postParams: postArray,
    };
    for (let i = 0; i < editArray.length; i++) {
      let element = editArray[i];
      for (let j = 0; j < postArray.length; j++) {
        let item = postArray[j];
        if (element.orderId == item.orderId) {
          data.postParams.splice (j, 1);
          // this.promised = true
          i = editArray.length;
          j = postArray.length;
          this.delectChange (editArray, orderArray, data);
        }
      }
    }
  },
  delectChange (editArray, orderArray, data) {
    for (let i = 0; i < editArray.length; i++) {
      let element = editArray[i];
      for (let j = 0; j < orderArray.length; j++) {
        let item = orderArray[j];
        if (element.orderId == item.id) {
          data.editParams.splice (i, 1);
          data.orderList.splice (j, 1);
          i = editArray.length;
          j = orderArray.length;

          // 返回订单页时的已选
          let selectOrder = data.postParams.map (el => el.orderId);
          wx.setStorageSync ('selectOrder', selectOrder);
          this.setData (data);
          util.toast ('删除成功');
        }
      }
    }
  },
  completeEdit: function () {
    util.preventMoreClick (this, () => {
      this.setData ({
        editState: false,
      });
    });
  },
  wxShare (e) {
    this.setData ({
      shareModual: false,
    });
  },
  // 切换隐藏公开价格
  switchHiddenPrice (e) {
    let statusprice = e.currentTarget.dataset.statusprice;
    let id = e.currentTarget.dataset.id;

    let data = {
      orderList: this.data.orderList,
      editParams: this.data.editParams,
      postParams: this.data.postParams,
    };
    data.editParams.forEach ((element, index) => {
      // data.orderList.map((item, num) => {
      if (element.orderId == id) {
        element.isHiddenPrice = !statusprice;
      }
      // })
    });
    data.postParams.forEach ((element, index) => {
      // data.orderList.map((item, num) => {
      if (element.orderId == id) {
        element.isHiddenPrice = !statusprice;
      }
      // })
    });
    data.orderList.map ((item, num) => {
      if (item.id == id) {
        item.hiddenPrice = !statusprice;
        if (item.hiddenPrice) {
          util.toast ('价格已隐藏');
        } else {
          util.toast ('价格已公开');
        }
      }
    });
    this.setData (data);
  },
  msgSend (e) {
    util.toast ('体验环境下不可用');
  },
  copyShortLink (e) {
    let customId = wx.getStorageSync ('customId');
    wx.showLoading ({
      title: '',
    });
    let that = this;
    // let shortLinkParams = this.shortLinkClone(this.data.postParams)
    let shortLinkParams = this.data.postParams.map (el => ({
      orderid: el.orderId,
      hiddenPrice: el.isHiddenPrice,
    }));
    let postBody = {
      customId: customId,
      customName: this.data.customName,
      // orderList: [{
      //   orderId: this.data.order.id,
      //   hiddenPrice: !this.data.pricePublic
      // }],
      orderList: shortLinkParams,
      // from: 'detail'
    };
    API.getShortLink (postBody).then (
      res => {
        wx.hideLoading ();
        wx.setClipboardData ({
          data: res.shortLink,
          success: function () {
            wx.hideLoading ();
            if (res.shortLink) {
              wx.showToast ({
                title: '短链接已复制',
                icon: 'none',
                duration: 1500,
              });
            } else {
              wx.showToast ({
                title: '链接获取失败',
                icon: 'none',
                duration: 1500,
              });
            }
            that.setData ({
              shareModual: false,
            });
          },
        });
      },
      err => {
        setTimeout (() => {
          wx.navigateBack ({
            delta: 1,
          });
        }, 1500);
      }
    );
  },
  arrayObjRemove: function (arr, elem) {
    let index = arr.indexOf (elem);
    if (index > -1) {
      arr.splice (index, 1);
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.hideShareMenu ();
    let selectOrder = wx.getStorageSync ('selectOrder');
    if (!selectOrder) {
      wx.reLaunch ({
        url: '/pages/home/home',
      });
    }
    this.getChooseOrderListById ();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    while (true) {
      if (this.data.shareId) break;
    }
    return {
      // title: app.globalData.user.companyName + '分享订单全链路详情',
      title: this.data.customName + '分享订单全链路详情',
      path: '/pages/share/share?shareId=' + this.data.shareId,
      imageUrl: '/images_keep/share5.png',
      success: function () {
        wx.showToast ({
          title: '分享成功',
        });
      },
      fail: function () {
        wx.showToast ({
          title: err || '分享失败',
        });
      },
    };
  },
});
